package com.crm.aftersale.controller;


import com.crm.aftersale.domain.Goods;
import com.crm.aftersale.domain.OrderGoods;
import com.crm.aftersale.domain.OrderPerson;
import com.crm.aftersale.domain.ReGoods;
import com.crm.aftersale.domain.query.ReGoodsQuery;
import com.crm.aftersale.domain.vo.ReGoodsVo;
import com.crm.aftersale.service.IGoodsService;
import com.crm.aftersale.service.IOrderGoodsService;
import com.crm.aftersale.service.IOrderPersonService;
import com.crm.aftersale.service.IReGoodsService;
import com.crm.base.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-22
 */
@RestController
@RequestMapping("ret")
public class ReGoodsController {

    @Autowired
    private IReGoodsService reGoodService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IOrderPersonService orderPersonService;
    @Autowired
    private IOrderGoodsService orderGoodsService;


    @PostMapping("select")
    public AjaxResult selectAll(@RequestBody(required = false) ReGoodsQuery reGoodsQuery, Integer current, Integer size) {

        reGoodsQuery.setCurrent(current);
        reGoodsQuery.setSize(size);
        String[] recordTime = reGoodsQuery.getRecordTime();
        String[] approveTime = reGoodsQuery.getApproveTime();
        System.out.println(reGoodsQuery);
        if (recordTime != null) {
            if (recordTime.length != 0) {
                reGoodsQuery.setStartTime1(recordTime[0].substring(0, 10));
                reGoodsQuery.setEndTime1(recordTime[1].substring(0, 10));
            }
        }
        if (approveTime != null) {
            if (approveTime.length != 0) {
                reGoodsQuery.setStartTime2(approveTime[0].substring(0, 10));
                reGoodsQuery.setEndTime2(approveTime[1].substring(0, 10));
            }
        }

//        System.out.println(reGoodsQuery);
//        System.out.println(reGoodsQuery.getCurrent());
//        System.out.println(reGoodsQuery.getSize());

        return reGoodService.findReGoods(reGoodsQuery);

    }

    @PostMapping("orderSelect")
    public AjaxResult selectAll(@RequestParam Integer orderId) {

        ReGoodsVo reGoodsVo = reGoodService.findByOrderId(orderId);

        return AjaxResult.me(reGoodsVo);
    }

    @PostMapping("selectGoods")
    public AjaxResult selectByGoodId() {

        List<Goods> goods = goodsService.findGoods();

        return AjaxResult.me(goods);
    }

    @PostMapping("orderAll")
    public AjaxResult selectOrderPerson() {

        List<OrderPerson> list = orderPersonService.findOrderPerson();
        return AjaxResult.me(list);
    }


    @PostMapping("insertReGoods")
    public AjaxResult insertReGoods(@RequestBody(required = false) ReGoods reGoods, HttpServletRequest request) {


        Integer i = reGoodService.addReGoods(reGoods, request);


        if (i != 0) {
            return AjaxResult.me("保存成功");
        } else {
            return AjaxResult.me("保存失败");
        }
    }


    @PostMapping("updateOG")
    public AjaxResult updateOG(@RequestBody(required = false) List<OrderGoods> orderGoods) {


        System.out.println(orderGoods);


        Integer i = orderGoodsService.updateByOb(orderGoods);


        if (i != 0) {
            return AjaxResult.me("保存成功");
        } else {
            return AjaxResult.me("保存失败");
        }

    }


    @PostMapping("updateReGoods")
    public AjaxResult updateReGoods(@RequestBody(required = false) ReGoods reGoods, HttpServletRequest request) {


        Integer i = reGoodService.updateReGoods(reGoods, request);


        if (i != 0) {
            return AjaxResult.me("保存成功");
        } else {
            return AjaxResult.me("保存失败");
        }

    }

    @PostMapping("updateType")

    public AjaxResult updateType(@RequestBody(required = false) ReGoods reGoods, HttpServletRequest request) {

        System.out.println(reGoods);
        Integer i = reGoodService.updateType(reGoods, request);


        if (i != 0) {
            return AjaxResult.me("保存成功");
        } else {
            return AjaxResult.me("保存失败");
        }

    }


    @PostMapping("deleteReGoods")
    public AjaxResult updateReGoods(@RequestBody(required = false) ReGoods reGoods) {


        Integer i = reGoodService.deleteReGoods(reGoods);


        if (i != 0) {
            return AjaxResult.me("删除成功");
        } else {
            return AjaxResult.me("删除失败");
        }
    }


    @PostMapping("fileDown")
    public AjaxResult fileDown(HttpServletResponse response)  {

        return reGoodService.fileDown(response);

    }
}
